package com.health.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.system.common.utils.UserAuthUtil;
import com.health.system.dto.EvaluationResultDTO;
import com.health.system.entity.HealthData;
import com.health.system.entity.HealthRules;
import com.health.system.entity.HealthSuggestions;
import com.health.system.entity.UserInfo;
import com.health.system.mapper.*;
import com.health.system.service.HealthDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-06-22
 */
@Service
public class HealthDataServiceImp extends ServiceImpl<HealthDataMapper, HealthData> implements HealthDataService {
    @Autowired
    private HealthDataMapper healthDataMapper;
    @Autowired
    private HealthRulesMapper healthRulesMapper;
    @Autowired
    private HealthSuggestionsMapper healthSuggestionsMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public IPage<HealthData> getAllHealthData(Long current, Long size) {
        IPage<HealthData> page = new Page<>(current, size);
        LambdaQueryWrapper<HealthData> wrapper = new LambdaQueryWrapper<>();
        if (roleMapper.selectRoleIdByUserId(UserAuthUtil.authId()) == 1) {
            wrapper.eq(HealthData::getUserId, UserAuthUtil.authId());
        }
        IPage<HealthData> result = healthDataMapper.selectPage(page, wrapper);

        for (HealthData record : result.getRecords()) {
            UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, record.getUserId()));
            record.setUserName(userInfo.getNickname() == null ? record.getUserName() : userInfo.getNickname());
        }

        return result;
    }

    @Override
    public void addHealthData(HealthData healthData) {
        healthData.setUserId(Math.toIntExact(UserAuthUtil.authId()));
        healthData.setCreateTime(LocalDateTime.now());
        healthData.setTotalScore(healthData.getSleepScore() + healthData.getStressScore() + healthData.getExerciseScore() + healthData.getDietScore() + healthData.getFatigueScore());
        healthDataMapper.insert(healthData);
    }

    @Override
    public void updateHealthData(HealthData healthData) {
        healthData.setTotalScore(healthData.getSleepScore() + healthData.getStressScore() + healthData.getExerciseScore() + healthData.getDietScore() + healthData.getFatigueScore());
        healthDataMapper.updateById(healthData);
    }

    @Override
    public void deleteHealthData(Integer id) {
        healthDataMapper.deleteById(id);
    }

    @Override
    public EvaluationResultDTO evaluationResult(Integer id) {
        // 判断区间
        HealthRules healthRules = healthRulesMapper.getStarsForScore(healthDataMapper.selectById(id).getTotalScore());
        System.out.println("规则数据：" + healthRules);

        // 获取建议
        List<HealthSuggestions> suggestions = healthSuggestionsMapper.selectList(new LambdaQueryWrapper<HealthSuggestions>()
                .eq(HealthSuggestions::getHealthRuleId, healthRules.getId()));

        String advice = "暂无建议";
        if (!suggestions.isEmpty()) {
            advice = suggestions.get(new Random().nextInt(suggestions.size())).getContent();
        }

        return EvaluationResultDTO.builder()
                .starts(healthRules.getStars())
                .advice(advice)
                .build();
    }
}
